package com.vgulu.server.common.utils;


import java.util.List;

public class Pager<T> {
	
	private int pageNo;
	
	private int pageSize;
	
	private int total; // 总记录数据
	
	private List<T> resultList;
	
	private int rangeSize = 10; // 页码显示范围
	
	private int pageCount = 1;
	
	public Pager() {
	}
	
	public Pager(List<T> resultList, int pageNo, int pageSize, int totalCount){
		this.resultList = resultList;
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.total = totalCount;
		calculatePageCount();
	}
	
	
	public int getPageCount() {
		return this.pageCount;
	}

	public int getPageNo() {
		return pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getTotal() {
		return total;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		calculatePageCount();
	}

	public void setTotal(int total) {
		this.total = total;
		calculatePageCount();
	}

	public void setResultList(List<T> resultList) {
		this.resultList = resultList;
	}

	public int getRangeSize() {
		return rangeSize;
	}

	public void setRangeSize(int rangeSize) {
		this.rangeSize = rangeSize;
	}

	public List<T> getResultList() {
		return resultList;
	}
	
	public int getMiddle() {
		return this.rangeSize / 2;
	}

	/**
	 * 获取页面区域第一项
	 * 
	 * @return
	 */
	public int getRangeOfFirst() {
		if (this.getPageCount() <= this.getRangeSize()) {
			return 1;
		}
		if (this.getPageNo() - this.getMiddle() <= 1) {
			return 1;
		}
		return this.getPageNo() - this.getMiddle();
	}

	/**
	 * 获取页面区域最后一项
	 * 
	 * @return
	 */
	public int getRangeOfEnd() {
		int end = this.getRangeOfFirst() - 1 + this.getRangeSize();
		if (end > this.getPageCount()) {
			return this.getPageCount();
		}
		return end;
	}
   
	private void calculatePageCount(){
		if(this.total > 0 && this.pageSize > 0){
			this.pageCount = (this.total-1) / this.pageSize + 1;
		}
	}
	
	
}
